package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppSystemService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppFeedback;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppSuggest;
import com.vf.admin.miaodong.service.AfterFeedbackRecordService;
import com.vf.admin.miaodong.service.AfterSuggestService;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 反馈举报管理
 */
@CrossOrigin
@Controller
@RequestMapping("/after/feedback")
public class AfterFeedbackController extends BaseController {

    @Autowired
    AfterFeedbackRecordService afterFeedbackRecordService;
    @Autowired
    AfterSuggestService afterSuggestService;
    @Autowired
    AppSystemService appSystemService;


    /**
     * 获取所有反馈举报列表信息
     *
     * @param pageBean 分页对象
     * @param nick     用户昵称
     * @param leixing  举报类型 1、动态  2、房间 ...
     * @param state    状态  1、未处理  2、已处理 默认 1未处理
     * @param userid   举报人
     * @param phone    用户手机号
     * @param start    时间范围-前
     * @param end      时间范围-后
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean pageBean, String nick, String leixing, String state, String userid, String phone, String start, String end) {
        try {
            //检索条件
            Map<String, Object> parMap = new HashMap<>();
            //用户昵称
            parMap.put("nick", nick);
            //用户手机号
            parMap.put("phone", phone);
            //举报类型
            parMap.put("leixing", leixing);
            //举报类型
            parMap.put("state", state);
            //用户id
            parMap.put("userid", userid);
            //时间范围-前
            parMap.put("startDate", start);
            //时间范围-后
            parMap.put("endDate", end);
            //根据检索条件查询数据
            List<AppFeedback> list = afterFeedbackRecordService.findList(parMap, pageBean);
            request.setAttribute("list", list);
            request.setAttribute("inv", parMap);
            request.setAttribute("splitPage", pageBean);
            request.setAttribute("nick", nick);
            request.setAttribute("userid", userid);
            request.setAttribute("leixing", leixing);
            request.setAttribute("state", state);
            request.setAttribute("phone", phone);
            request.setAttribute("start", start);
            request.setAttribute("end", end);
            return this.sucess("/web/after/feedback/list");
        } catch (Exception e) {

            return this.error("系统异常");
        }
    }

    /**
     * 详情跳转
     *
     * @param id id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            if (id == null || id <= 0) {
                return this.error("id不能为空");
            }
            List<Object> imgList = new ArrayList<>();
            AppFeedback feedback = afterFeedbackRecordService.get(id);
            if (!StringEx.isNull(feedback.getImg())) {
                String[] feedbackImg = feedback.getImg().split(",");
                for (int i = 0; i < feedbackImg.length; i++) {
                    imgList.add(feedbackImg[i]);
                }
            }
            request.setAttribute("feedback", feedback);
            request.setAttribute("imgList", imgList);
            return this.sucess("/web/after/feedback/edit");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }


    /**
     * 申诉处理
     *
     * @param idArray 举报反馈id
     * @param remark  处理备注
     * @return
     */
    @RequestMapping("/chuli")
    @ResponseBody
    public Result chuli(String idArray, String remark, HttpServletRequest request) {
        try {
            if (StringEx.isNull(idArray)) {
                return this.errorJSON("举报id不能为空!");
            }
            AppFeedback feedback = afterFeedbackRecordService.get(Integer.valueOf(idArray));
            if (StringUtils.isEmpty(feedback)) {
                return this.errorJSON("举报数据不存在!");
            }
            afterFeedbackRecordService.chuli(Integer.valueOf(idArray), remark);

            AppMessageRecord param = new AppMessageRecord();
            param.setState(1);
            param.setMessage("您的举报已经处理！");
            param.setTitle("举报反馈");
            param.setImage("http://yunliaos.oss-cn-beijing.aliyuncs.com/1638755345601i73wq965vHjMy.jpg");
            param.setLeixing(2);
            param.setUserid(feedback.getUserid());
            param.setCreatetime(new Date());
            return this.sucess();
        } catch (Exception e) {
            return this.error();
        }
    }


    /**
     * 获取所有反馈列表信息
     *
     * @param pageBean 分页对象
     * @param nick     用户昵称
     * @param phone    用户手机号
     * @param start    时间范围-前
     * @param end      时间范围-后
     * @return
     */
    @RequestMapping("/fklist")
    public String fklist(SplitPageBean pageBean, String nick, String phone, String start, String end) {
        try {
            //检索条件
            Map<String, Object> parMap = new HashMap<>();
            //用户昵称
            parMap.put("nick", nick);
            //用户手机号
            parMap.put("phone", phone);
            //时间范围-前
            parMap.put("startDate", start);
            //时间范围-后
            parMap.put("endDate", end);
            //根据检索条件查询数据
            List<AppSuggest> appSuggestList = afterSuggestService.findList(parMap, pageBean);
            request.setAttribute("list", appSuggestList);
            request.setAttribute("splitPage", pageBean);
            request.setAttribute("nick",nick);
            request.setAttribute("phone",phone);
            request.setAttribute("startDate",start);
            request.setAttribute("endDate",end);
            return this.sucess("/web/after/feedback/fklist");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统异常");
        }
    }


}
